﻿using System;
using System.Reflection;
using Mbs.ComponentModel.Container;

namespace Mbs.Modularity
{
    /// <summary>
    /// 模块提供者接口
    /// </summary>
    /// <remarks>采用抽象工厂设计模式</remarks>
    public interface  IModuleProvider
    {
        IModuleMeta CreateModuleMeta(string assemblyFile);
        IModuleMeta CreateModuleMeta(Assembly assembly);
        IModule CreateModule(IModuleMeta meta);
        IModuleActivatorProcessor CreateModuleActivatorProcessor(IModuleMeta meta);
    }

    [Serializable]
    [Component(Service = typeof(IModuleProvider))]
    public class ModuleProvider : MarshalByRefObject, IModuleProvider
    {
        #region IModuleProvider Members

        public virtual IModuleMeta CreateModuleMeta(string assemblyFile)
        {
            if (!string.IsNullOrEmpty(assemblyFile))
                return ModuleMeta.Load(assemblyFile, false);
            return null;
        }

        public virtual IModuleMeta CreateModuleMeta(Assembly assembly)
        {
            return ModuleMeta.Load(assembly, false);
        }

        public virtual IModule CreateModule(IModuleMeta meta)
        {
            return new Module(meta);
        }

        public virtual IModuleActivatorProcessor CreateModuleActivatorProcessor(IModuleMeta meta)
        {
            return new ConfigurationModuleActivatorProcessor(meta);
        }

        

        #endregion
    }
}
